home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3406 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  105 lines

  1. Path: quasar.engr.sgi.com!davea
  2. From: davea@quasar.engr.sgi.com (David B.Anderson)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Nested Structures in C - A Question
  5. Date: 28 Jan 1996 19:17:42 GMT
  6. Organization: Silicon Graphics, Inc., Mountain View, CA
  7. Message-ID: <4egi4m$4ik@fido.asd.sgi.com>
  8. References: <36400002@peg>
  9. NNTP-Posting-Host: quasar.engr.sgi.com
  10.  
  11. In article <36400002@peg>,  <tmccoy@peg.apc.org> wrote:
  12. >A Question on Nested Structures in C
  13. >struct outer {
  14. >    int var1;
  15. >    int var2;
  16. >    int var3;
  17. >};
  18. >
  19. >Apparently the compiler will NOT allocate any storage when
  20. >I do this because, according to Kernighan and Ritchie (2nd
  21. >Ed), "a structure declaration that is not followed by a
  22. >list of variables reserves no storage; it merely describes
  23. >a template or the shape of a structure" (Page 128).
  24. >
  25.  
  26. >struct outer {
  27. >    int var1;
  28. >    int var2;
  29. >    int var3;
  30. >    struct inner {
  31. >        int nested1;
  32. >        int nested2;
  33. >    };
  34. >};
  35.  
  36. >and I should be able to define an instance of my structure
  37. >by doing:
  38. >
  39. >struct outer instance;
  40. >
  41. >and refer to the first member of my inner structure by doing:
  42. >
  43. >instance.inner.nested1
  44. >
  45. >Yet, C won't let me do this!!  It insists that I define my
  46. >nested structure as follows:
  47. [long discussion about the above deleted to save space]
  48.  
  49. The declaration 
  50. struct outer {
  51.        int var1;
  52.        int var2;
  53.        int var3;
  54.        struct inner {
  55.                int nested1;
  56.                int nested2;
  57.        };
  58. };
  59. declares a struct outer with 3 members, var1, var2, var3.
  60. Because, in ISO C, struct outer { ...} is not a scope,
  61. and because there is no 'instance of inner which is a member
  62. of outer'
  63. this is identical to the following pair of declarations:
  64.  
  65.  
  66.   struct outer {
  67.        int var1;
  68.        int var2;
  69.        int var3;
  70.     /* no instance  of 'inner' */
  71.   };
  72.   /* no scope, so 'inner' is really a struct just as visible as 'outer'. */
  73.   struct inner {
  74.                int nested1;
  75.                int nested2;
  76.   };
  77.  
  78. (Identical aside from the question of when 'struct inner' becomes visisble
  79. during compilation of the declarations).
  80.  
  81. If you want to refer to an 'inner' in 'outer' you must declare such.
  82. Here is a clearer (for C) way to do this, one which reflects the
  83. true declaration scope and creates an 'inner' member in 'outer'.
  84.  
  85.   struct inner {
  86.                int nested1;
  87.                int nested2;
  88.   };
  89.   struct outer {
  90.        int var1;
  91.        int var2;
  92.        int var3;
  93.        struct inner var4; /* here we have an 'inner' member */
  94.   };
  95.  
  96. struct outer instance2;
  97.     instance2.var4.nested1 = 0;
  98.  
  99. Note that in C++, things are quite different: each struct/class *is*
  100. a scope, so the above would not be a correct analysis for C++.
  101.  
  102. Hope this makes sense.
  103. [ David B. Anderson              (415)933-4263            davea@sgi.com      ]
  104. [ Suddenly to me that no verb in this sentence.                        -m.toy]
  105.